async function checkIsIos() {
	return new Promise((resolve => {
		uni.getSystemInfo({
			success: function(res) {
				resolve(res.platform === "ios")
			}
		});
	}))
}

async function showAuthTipModal(authorize) {
	// #ifndef APP-PLUS
	if (1 === 1) {
		return Promise.resolve(true);
	}

	// #endif
	// ios端在manifest.json配置权限使用说明，以下权限判断仅在安卓端可用 
	let isIos = await checkIsIos()
	if (isIos) return Promise.resolve(true);
	if (checkPremission(authorize)) return Promise.resolve(true);
	// 如果已经授权直接返回 
	const contentData = {
		['android.permission.ACCESS_FINE_LOCATION']: {
			title: "定位权限说明",
			describe: "便于您使用该功能发送您的位置消息，请您确认或手动授权， 否则无法使用该功能 "
		},
		["android.permission.READ_EXTERNAL_STORAGE"]: {
			title: "相册权限说明",
			describe: "便于您使用该功能发送图片/视频消息，请您确认或手动授权， 否则无法使用该功能 "
		},
		["android.permission.CAMERA"]: {
			title: "拍摄权限说明",
			describe: "便于您使用该功能拍摄照片、录制视频、拨打视频电话，请您确认或手动授权， 否则无法使用该功能 "
		},
		["android.permission.RECORD_AUDIO"]: {
			title: "录音权限说明",
			describe: "便于您使用该功能录制并发送语音消息，请您确认或手动授权， 否则无法使用该功能 "
		}
	}
	return new Promise((resolve) => {
		uni.showModal({
			title: contentData[authorize].title,
			content: contentData[authorize].describe + '\n\n',
			success: (res) => {
				resolve(!!res.confirm)
			},
		})
	})
}
async function showManualAuth(authorize) {
	let isIos = await checkIsIos()
	if (isIos) return true
	const contentData = {
		['android.permission.ACCESS_FINE_LOCATION']: "获取定位权限失败，请手动打开授权或检查系统定位开关",
		["android.permission.READ_EXTERNAL_STORAGE"]: "获取相册权限失败，请手动打开授权",
		["android.permission.CAMERA"]: "获取拍摄权限失败，请手动打开授权",
		["android.permission.CALL_PHONE"]: "获取拨打电话权限失败，请手动打开授权",
	}
	uni.showModal({
		title: '提示',
		content: contentData[authorize],
		confirmText: "去设置",
		success: (res) => {
			if (res.confirm) {
				uni.openAppAuthorizeSetting({
					success(res) {
						console.log(res);
					}
				});
			}
			if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}

function checkPremission(authorize) {
	let compat = plus.android.importClass('androidx.core.content.ContextCompat');
	let context = plus.android.runtimeMainActivity();
	let result = compat.checkSelfPermission(context, authorize);
	if (result === 0) {
		return true;
	} else {
		return false;
	}
}
export {
	showAuthTipModal,
	showManualAuth,
	checkPremission,
}